﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections.Generic;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Admin
{
    public partial class UpdateProcureCatalog : BasePage
    {
        private IBLL_ProcurementBasic ibp = Factory.BLL_ProcurementBasic();
        public UpdateProcureCatalog()
        {
            this.ModuleNo = Module.产品信息.ToString();
        }
        private string PricePlanSerialID = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            //绑定需要修改的调价计划的信息
            if (!IsPostBack)
            {
                //
                PricePlanSerialID = Request.QueryString["GoodsId"];
                if (string.IsNullOrEmpty(PricePlanSerialID))
                {
                    PageError("参数错误！", this.ReturnUrl);
                }

                //获取查询条件
                Model.Query.ProcureCatalog procureCatalog = new HZ.DrugTrade.Model.Query.ProcureCatalog();
                procureCatalog.GoodsID = PricePlanSerialID;


                int totalCount = 0;
                IBLL_ProcurementBasic ipb = Factory.BLL_ProcurementBasic();


                IList<Model.View.ProcureCatalog> AdjustPricePlanList = ipb.GetProcureCatalogByWhere(procureCatalog, 1, 0, out totalCount);

                ViewState["Projectid"] = string.IsNullOrEmpty(AdjustPricePlanList[0].ProjectID) ? string.Empty : AdjustPricePlanList[0].ProjectID;
                txtCompany_SC.Text = AdjustPricePlanList[0].CompanyName_SC;
                txtCompany_TB.Text = AdjustPricePlanList[0].CompanyName_TB;
                txtGoodsName.Text = AdjustPricePlanList[0].GoodsName;
                txtMaterial.Text = AdjustPricePlanList[0].Material;
                txtOutLook.Text = AdjustPricePlanList[0].Outlookc;
                if (ViewState["Projectid"].ToString() == "0002")
                {
                    lblBidPrice1.Text = "自主报价";
                }
                lblBidPrice.Text = ViewState["Projectid"].ToString() == "0002" ? Convert.ToDecimal(AdjustPricePlanList[0].ProcurePrice.ToString()).ToString("F2") : Convert.ToDecimal(AdjustPricePlanList[0].BidPrice.ToString()).ToString("F4"); //中标价格
                lblPackPrice.Text = ViewState["Projectid"].ToString() == "0002" ? Convert.ToDecimal(Convert.ToDecimal(AdjustPricePlanList[0].ProcurePrice)).ToString("F2") : Convert.ToDecimal((Convert.ToDecimal(AdjustPricePlanList[0].BidPrice) * (AdjustPricePlanList[0].PackCount == null ? 0 : AdjustPricePlanList[0].PackCount == "" ? 0 : Convert.ToInt32(AdjustPricePlanList[0].PackCount))).ToString()).ToString("F4"); //最小零售包装价格
                txtPackUnit.Text = AdjustPricePlanList[0].PackUnit.ToString();
                txtPackCount.Text = AdjustPricePlanList[0].PackCount.ToString();
                txtGoodsName.Text = AdjustPricePlanList[0].GoodsName;
                txtUnit.Text = AdjustPricePlanList[0].Unit;
                if (CurrentAccount.GetUser().UserRole.RoleID == "05")
                {
                    if (AdjustPricePlanList[0].SubmitStatus == 1 && AdjustPricePlanList[0].CheckStatus != 2)
                    {
                        txtPackUnit.Enabled = false;
                        txtPackCount.Enabled = false;
                        btnUpdate.Enabled = false;
                        btnSubmit.Enabled = false;
                    }
                }
                if (CurrentAccount.GetUser().UserRole.RoleID == "03")
                {
                    btnSubmit.Visible = false;
                }
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnUpdate_Click(object sender, EventArgs e)
        {

            if (Page.IsValid)
            {
                //获取修改的信息
                Model.View.ProcureCatalog list = new Model.View.ProcureCatalog();
                list.GoodsID = Request.QueryString["GoodsId"].ToString();
                if (txtPackUnit.Text.Trim() != "")
                {
                    list.PackUnit = txtPackUnit.Text.Trim();
                }
                else
                {
                    HZ.Web.WebUtil.Alert("包装单位不能为空");
                    return;
                }
                if (txtPackCount.Text.Trim() != "")
                {
                    list.PackCount = txtPackCount.Text.Trim();
                }
                else
                {
                    HZ.Web.WebUtil.Alert("最小零售数量不能为空");
                    return;
                }

                if (ibp.UpdateProcureCatalogList(list) == 1)
                {
                    PageInfo("修改成功！", "UpdateProcureCatalog.aspx?GoodsId=" + list.GoodsID);
                }
                else
                {
                    PageError("修改失败！", "");
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string ID = Request.QueryString["GoodsId"].ToString();
            Model.View.ProcureCatalog list = new Model.View.ProcureCatalog();
            list.GoodsID = ID;
            if (txtPackUnit.Text.Trim() != "")
            {
                list.PackUnit = txtPackUnit.Text.Trim();
            }
            else
            {
                HZ.Web.WebUtil.Alert("包装单位不能为空");
                return;
            }
            if (txtPackCount.Text.Trim() != "")
            {
                list.PackCount = txtPackCount.Text.Trim();
            }
            else
            {
                HZ.Web.WebUtil.Alert("最小零售数量不能为空");
                return;
            }
            if (ibp.UpdateProcureCatalogList(list) == 1)
            {
                if (ibp.SubmitProcureCatalogList(ID, 0, "") == 1)
                {
                    HZ.Web.WebUtil.Alert("提交成功！");
                }
                else
                {
                    PageError("数据异常！", "");
                }

            }
            else
            {
                PageError("数据异常！", "");
            }

        }

        protected void btnback_Click(object sender, EventArgs e)
        {
            if (CurrentAccount.GetUser().UserRole.RoleID == "05")  //生产企业
            {
                Response.Redirect("UpdateProcureCatalogList.aspx");
            }
            if (CurrentAccount.GetUser().UserRole.RoleID == "03")  //中心
            {
                Response.Redirect("/Admin/DrugLibManage/CheckProcureCatalogList.aspx");
            }

        }
    }
}
